using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using CSTControls;
namespace WindowsApplication1
{
    public partial class Form3 : Form
    {

        cstTextBox[] cbAnswers = new cstTextBox[2000];
        cstTextBox[] gLabel = new cstTextBox[200];
        private void dd()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"d:\Test.xml");

            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                ProcesNode(node, doc.DocumentElement.Name);
            }
        }


        private void ProcesNode(XmlNode node, string parentPath)
        {
            if (!node.HasChildNodes
                || ((node.ChildNodes.Count == 1) && (node.FirstChild is System.Xml.XmlText)))
            {
                System.Diagnostics.Debug.WriteLine(parentPath + "/" + node.Name);

                //tr.WriteLine(" <fo:block xsl:use-attribute-sets=\"PageTitle\">");
                //tr.WriteLine("<xsl:value-of select=\"" + parentPath + "/" + node.Name + "\"></xsl:value-of>");
                ////   tr.Write(parentPath + "/" + node.Name);

                //tr.WriteLine("</fo:block >");




            }
            else
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    ProcesNode(child, parentPath + "/" + node.Name);
                }
            }
        }

        private void ddd(string filename)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filename);

            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
                ProcesNoded(node, doc.DocumentElement.Name);
            }
        }

        int sub = 0;
        private void ProcesNoded(XmlNode node, string parentPath)
        {
            if (!node.HasChildNodes
                || ((node.ChildNodes.Count == 1) && (node.FirstChild is System.Xml.XmlText)))
            {

                cbAnswers[sub] = new cstTextBox();
                cbAnswers[sub].AutoSize = true;
                flowLayoutPanel1.Controls.Add(cbAnswers[sub]);
                cbAnswers[sub].Parent = flowLayoutPanel1;
                cbAnswers[sub].TabStop = true;
                cbAnswers[sub].Width  = flowLayoutPanel1 .Width ;
                cbAnswers[sub].MouseDown += sourcectrl_MouseDown;
                this.cbAnswers[sub].Text = parentPath + "/" + node.Name;



                sub++;



            }
            else
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    ProcesNoded(child, parentPath + "/" + node.Name);
                }
            }
        }






        public Form3()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            
            flowLayoutPanel1.Controls.Clear();
            try
            {
                ddd(@"d:\Test.xml");
            }
            catch
            {
                OpenFileDialog of = new OpenFileDialog();
                if (of.ShowDialog() == DialogResult.OK)
                {
                    ddd(of.FileName);
                  
                }

            }





            

            
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            try
            {
                string fp = Properties.Settings.Default.XMLpath;
                DisplayXmlFile(fp, treeView1);
            }
            catch { }
        }

        private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
        {
            string s = "";
        }
        int rows = 1;
        int cols = 1;





        int gsub = 0;
        private void addAgridLable(int row, int col, string str)
        {

            gLabel[gsub] = new cstTextBox ();
           
            gLabel[gsub].Name = "grid_" + row.ToString() +"_"+ col.ToString();
            gLabel[gsub].AutoSize = false ;
            gLabel[gsub].Height = 30;
            gLabel[gsub].Width  = 110;

            tableLayoutPanel1.Controls.Add(gLabel[gsub],row, col);
            gLabel[gsub].Parent = tableLayoutPanel1;
            gLabel[gsub].TabStop = true;
            gLabel[gsub].borderSize  = 1;
            gLabel[gsub].borderSide = bordersides.All;
            gLabel[gsub].BackColor = Color.Goldenrod;
           
            //gLabel[gsub].Dock = System.Windows.Forms.DockStyle.Fill;
            gLabel[gsub].DragDrop += destctrl_DragDrop;
            gLabel[gsub].DragOver += destctrl_DragOver;
            gLabel[gsub].MouseEnter  += destctrl_MouseEnter;
           
            gLabel[gsub].Font = textBox1.Font ;

            fop temp = new fop();
            temp.borderThickness = 1;
            temp.border = bordersides.All;

            gLabel[gsub].Tag =
            temp;

           // gLabel[gsub].BorderStyle = BorderStyle.FixedSingle;

            gLabel[gsub].AllowDrop = true;
            gLabel[gsub].ContextMenuStrip  = contextMenuStrip1 ;
            this.gLabel[gsub].Text = str;



            gsub++;

        }


        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                rows = Convert.ToInt16(textBox1.Text);
                cols = Convert.ToInt16(textBox2.Text);
            }
            catch
            {
                MessageBox.Show("Please give valid rows and columns number");
                return;
            }
            //if (rows > 8)
            //{
            //    MessageBox.Show("Less than 8 rows");
            //    return;
            //}
            if (cols > 20)
            {
                MessageBox.Show("Less than 20 cols");
                return;
            }

            tableLayoutPanel1.Controls.Clear();
            gLabel = new cstTextBox[2000];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    addAgridLable(j, i, "");
                }
            }
        }
        public Color GetRandomColor()
        {
            Random rnd = new Random((int)DateTime.Now.Millisecond);
            byte r = Convert.ToByte(rnd.Next(0, 255));
            byte g = Convert.ToByte(rnd.Next(0, 255));
            byte b = Convert.ToByte(rnd.Next(0, 255));

            return Color.FromArgb(r, g, b);
        }

        Color dropcolor;
        private void sourcectrl_MouseDown(object sender, MouseEventArgs e)
        {
            cstTextBox ctrl = new cstTextBox();
            ctrl = (cstTextBox)sender;

            DragDropEffects dde1 = DoDragDrop(ctrl.Text, DragDropEffects.All);

            if (dde1 == DragDropEffects.All)
            {
                ctrl.BackColor = dropcolor;
            }

        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                DragDropEffects dde1 = DoDragDrop(treeView1.SelectedNode.FullPath .ToString ().Remove (0,1).ToString (), DragDropEffects.All);
                if (dde1 == DragDropEffects.All)
                {
                    treeView1.SelectedNode.BackColor = dropcolor;
                }
            }
            catch { }
        }


        private void destctrl_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string str = (string)e.Data.GetData(DataFormats.StringFormat);
                cstTextBox ctrl = new cstTextBox();
                ctrl = (cstTextBox)sender;
                try
                {
                    ctrl.Text = str.Substring(str.LastIndexOf("\\")+1, str.Length - str.LastIndexOf("\\")-1);
                }
                catch { ctrl.Text = str; }
                str = str.Replace("document","");

                fop newfop = new fop();
                newfop.xmlNode = str.Replace("\\", "/");
                ctrl.Tag = newfop;
                dropcolor = GetRandomColor();
                ctrl.BackColor = dropcolor;
            }
        }

        private void destctrl_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }
        StringBuilder sb = new StringBuilder();
        private void button2_Click(object sender, EventArgs e)
        {
            sb.Remove(0, sb.Length);
            sb.AppendLine("<fo:table table-layout=\"fixed\" width =\"7.0\">");
            for (int j = 0; j < cols; j++)
            {
                sb.AppendLine("<fo:table-column column-width=\"3.5in\"/>");
            }
           
          
            sb.AppendLine(" <fo:table-body>");


            //loop
            for (int i = 0; i < rows; i++)
            {
                sb.AppendLine(" <fo:table-row>");
                for (int j = 0; j < cols; j++)
                {
                    cstTextBox lbl = new cstTextBox();
                    lbl = (cstTextBox)tableLayoutPanel1.Controls["grid_" + j.ToString() + "_" + i.ToString()];
                        try
                        {
                            fop newfop = new fop();
                            newfop = (fop)lbl.Tag;
                            int bordersize = newfop.borderThickness;
                         
                           
                            switch (newfop.border)
                            {
                                case bordersides.All:
                                    sb.AppendFormat("<fo:table-cell border=\"{0}pt solid black\">", bordersize);
                                    break;
                                case bordersides.None:
                                    sb.AppendLine("<fo:table-cell>");
                                    break;
                                case bordersides.Left:
                                    sb.AppendFormat("<fo:table-cell border-left=\"{0}pt solid black\">", bordersize);
                                    break;
                                case bordersides.Right:
                                    sb.AppendFormat("<fo:table-cell border-right=\"{0}pt solid black\">", bordersize);
                                    break;
                                case bordersides.Top:
                                    sb.AppendFormat("<fo:table-cell border-top=\"{0}pt solid black\">", bordersize);
                                    break;
                                case bordersides.Bottom:
                                    sb.AppendFormat("<fo:table-cell border-bottom=\"{0}pt solid black\">", bordersize);
                                    break;

                            }

                            switch (lbl.Font .Bold )
                            {
                                case true :
                                    sb.AppendFormat(" <fo:block font-weight=\"bold\">",bordersize);
                                    break;
                                case false  :
                                    sb.AppendLine(" <fo:block>");
                                    break;
                            }
                
                            if (lbl.Tag == null || newfop.xmlNode.ToString().Trim() == "")
                            {
                            sb.AppendLine("");
                            }
                            else
                            {
                               sb.AppendLine("<xsl:value-of select=\"" + newfop.xmlNode .ToString () + "\"></xsl:value-of>");
                            }
                        }
                        catch 
                        {
                            sb.AppendLine("");
                        }

                     


                    sb.AppendLine(" </fo:block>");
                    sb.AppendLine("  </fo:table-cell>");
               
                    
                }
                sb.AppendLine(" </fo:table-row>");
            }
            
           
          
           
            //loop


            sb.AppendLine("  </fo:table-body>");
            sb.AppendLine("</fo:table>");

          //  tableLayoutPanel1.Controls[""];

            Clipboard.SetText(sb.ToString ());

            
        }

        private void destctrl_MouseEnter(object sender, EventArgs e)
        {
            cstTextBox ctrl = new cstTextBox();
            ctrl = (cstTextBox)sender;
            try
            {
                fop newfop = new fop();
                newfop = (fop)ctrl.Tag;

                toolTip1.Show(newfop .xmlNode , this , 0, 0,500);
            }
            catch { }
        }

        //private void destctrl_MouseDown(object sender, MouseEventArgs e)
        //{
        //    if (e.Button == MouseButtons.Right)
        //    {
        //        Label ctrl = new Label();
        //        ctrl = (Label)sender;
        //        ctrl.Text = "";
        //    }
        //}
        private void button3_Click(object sender, EventArgs e)
        {
          
                OpenFileDialog of = new OpenFileDialog();
                if (of.ShowDialog() == DialogResult.OK)
                {
                    treeView1.Nodes.Clear();
                    Properties.Settings.Default.XMLpath = of.FileName;
                    Properties.Settings.Default.Save();
                    DisplayXmlFile(of.FileName , treeView1);
                }
            
        

        }



        public void DisplayXmlFile(string filename, TreeView tvw)
        {
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(filename);
                // Add it to the TreeView Nodes collection
                DisplayXmlNode(xmldoc, tvw.Nodes);
                // Expand the root node.
                tvw.Nodes[0].Expand();
            }
            catch
            { MessageBox.Show("Invalid XML file"); }
        }

        public void DisplayXmlNode(XmlNode xmlnode, TreeNodeCollection nodes)
        {
            // Add a TreeView node for this XmlNode.
            // (Using the node's Name is OK for most XmlNode types.)
            TreeNode tvNode = nodes.Add(xmlnode.Name);

            switch (xmlnode.NodeType)
            {
                case XmlNodeType.Element:
                    // This is an element: Check whether there are attributes.
                    if (xmlnode.Attributes.Count > 0)
                    {
                        // Create an ATTRIBUTES node.
                        TreeNode attrNode = tvNode.Nodes.Add("(ATTRIBUTES)");
                        // Add all the attributes as children of the new node.
                        XmlAttribute xmlAttra = default(XmlAttribute);
                        foreach (XmlAttribute  xmlAttr in xmlnode.Attributes)
                        {
                            // Each node shows name and value.
                            attrNode.Nodes.Add(xmlAttr.Name + " = '" + xmlAttr.Value + "'");
                        }
                    }

                    break;
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    // For these node types we display the value
                    tvNode.Text = xmlnode.Value;
                    break;
                case XmlNodeType.Comment:
                    tvNode.Text = "<!--" + xmlnode.Value + "-->";
                    break;
                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.XmlDeclaration:
                    tvNode.Text = "<?" + xmlnode.Name + " " + xmlnode.Value + "?>";
                    break;
                default:
                    break;
                // ignore other node types.
            }

            // Call this routine recursively for each child node.
            XmlNode xmlChild = xmlnode.FirstChild;
            while (!(xmlChild == null))
            {
                DisplayXmlNode(xmlChild, tvNode.Nodes);
                xmlChild = xmlChild.NextSibling;
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {


            if (this.ActiveControl is cstTextBox )
            {
                cstTextBox ctrl = new cstTextBox();
                ctrl = (cstTextBox)this.ActiveControl;
                ctrl.Text = "";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            treeView1.CollapseAll();
            FindMatchingNode(textBox3.Text , treeView1.Nodes);
        }
        void FindMatchingNode(string text, TreeNodeCollection nodes)
        {
           
            foreach (TreeNode node in nodes)
            {
                if (node.Text.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) != -1)
                {
                    node.EnsureVisible();
                    node.ForeColor = Color.Red;
                    //break;
                }
                else { FindMatchingNode(text, node.Nodes);
                node.ForeColor  = Color.Black ;
            }// recursively search this node's children
            }
        }

        private void enterTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 f = new Form4();
            if (f.ShowDialog() == DialogResult.OK)
            {
                if (this.ActiveControl is cstTextBox)
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    ctrl.Text = f.textBox1.Text;
                }
            }
        }

        private void borderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 f = new Form4();
            if (f.ShowDialog() == DialogResult.OK)
            {
                if (this.ActiveControl is cstTextBox)
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    ctrl.Text = f.textBox1.Text;
                }
            }
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 f = new Form4();
            if (f.ShowDialog() == DialogResult.OK)
            {
                if (this.ActiveControl is cstTextBox)
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    ctrl.Text = f.textBox1.Text;
                }
            }
        }

        

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode  == Keys .Enter )
            {
                treeView1.CollapseAll();
                FindMatchingNode(textBox3.Text, treeView1.Nodes);
            }
        }

        private void btnBold_Click(object sender, EventArgs e)
        {
          
                if (this.ActiveControl is cstTextBox)
                {
                    try
                    {
                        cstTextBox ctrl = new cstTextBox();
                        ctrl = (cstTextBox)this.ActiveControl;
                         

                        fop newfop = new fop();
                       

                        FontDialog fd = new FontDialog();
                        if (fd.ShowDialog() == DialogResult.OK)
                        {
                            ctrl.Font = fd.Font;
                        }
                    }
                    catch { }
                }
             
        }

        private void btnBorder_Click(object sender, EventArgs e)
        {
            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    
                    fop newfop = new fop();
                    newfop = (fop)ctrl.Tag;
                    newfop.border = bordersides.All;
                    ctrl.Tag = newfop;
                    ctrl.borderSide = bordersides.All;
                    ctrl.Refresh();
                }
                catch { }
            }
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            frmProperties frm = new frmProperties();

            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    fop newfop = new fop();

                    newfop = (fop)ctrl.Tag;


                    frm.propertyGrid1.SelectedObject = newfop;



                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        ctrl.Tag = frm.propertyGrid1.SelectedObject;
                        ctrl.borderSide = newfop.border;
                        ctrl.borderSize = newfop.borderThickness;
                        ctrl.Refresh();

                    }
                }
                catch { }
            }
        }

        private void btnNone_Click(object sender, EventArgs e)
        {
            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    fop newfop = new fop();
                    newfop = (fop)ctrl.Tag;
                    newfop.border = bordersides.None;
                    ctrl.Tag = newfop;
                    ctrl.borderSide = bordersides.None;
                    ctrl.Refresh();
                }
                catch { }
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    fop newfop = new fop();
                    newfop = (fop)ctrl.Tag;
                    newfop.border = bordersides.Left ;
                    ctrl.Tag = newfop;
                    ctrl.borderSide = bordersides.Left ;
                    ctrl.Refresh();
                }
                catch { }
            }
        }

        private void btnBottom_Click(object sender, EventArgs e)
        {
            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    fop newfop = new fop();
                    newfop = (fop)ctrl.Tag;
                    newfop.border = bordersides.Bottom ;
                    ctrl.Tag = newfop;
                    ctrl.borderSide = bordersides.Bottom;
                    ctrl.Refresh();
                }
                catch { }
            }
        }

        private void btnRihght_Click(object sender, EventArgs e)
        {
            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    fop newfop = new fop();
                    newfop = (fop)ctrl.Tag;
                    newfop.border = bordersides.Right ;
                    ctrl.Tag = newfop;
                    ctrl.borderSide = bordersides.Right;
                    ctrl.Refresh();
                }
                catch { }
            }
        }

        private void btnTop_Click(object sender, EventArgs e)
        {
            if (this.ActiveControl is cstTextBox)
            {
                try
                {
                    cstTextBox ctrl = new cstTextBox();
                    ctrl = (cstTextBox)this.ActiveControl;
                    fop newfop = new fop();
                    newfop = (fop)ctrl.Tag;
                    newfop.border = bordersides.Top ;
                    ctrl.Tag = newfop;
                    ctrl.borderSide = bordersides.Top ;
                    ctrl.Refresh();
                }
                catch { }
            }
        }

       
       

       

    }


    public class fop
    {

        private int _borderThickness = 0;
        public int borderThickness
        {
            get
            {
                return _borderThickness;
            }
            set
            {
                _borderThickness = value;
            }
        }
        private bordersides _border = bordersides.All ;
        public bordersides border
        {
            get
            {
                return _border;
            }
            set
            {
                _border = value;
            }
        }

        private string _xmlNode = "";
        public string xmlNode
        {
            get
            {
                return _xmlNode;
            }
            set
            {
                _xmlNode = value;
            }
        }
    }






}